package com.aihuishou.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Author Zhang Jintao
 * @Date 2021/3/20 16:03
 * @Version 1.0
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class Knife4jConfig {

    private final ServerProperties serverProperties;
    private final SysInfoProperties sysInfoProperties;

    public Knife4jConfig(ServerProperties serverProperties, SysInfoProperties sysInfoProperties) {
        this.serverProperties = serverProperties;
        this.sysInfoProperties = sysInfoProperties;
    }


    private ApiInfo apiInfo() {
        String serviceUrl = "http://localhost:" + serverProperties.getPort() +
                serverProperties.getServlet().getContextPath();
        return new ApiInfoBuilder()
                .title("xxxx服务后台接口文档")
                .description("xxxx服务")
                .termsOfServiceUrl(serviceUrl)
                .version(sysInfoProperties.getVersion())
                .contact(new Contact(sysInfoProperties.getPic(), null, "wq6e54@123.com"))
                .build();
    }

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //分组名称
                .groupName("1.X版本接口")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.aihuishou.web"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

}
